package ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
import util.ConnectionUtils;

public class MedicalRecordDialog extends JDialog {
    private String patientId;
    private JTable recordTable;
    private DefaultTableModel tableModel;

    public MedicalRecordDialog(JFrame parent, String patientId) {
        super(parent, "就医记录", true);
        this.patientId = patientId;
        
        setSize(800, 600);
        setLocationRelativeTo(parent);
        setLayout(new BorderLayout(10, 10));
        
        // 创建表格
        String[] columns = {"记录编号", "就诊时间", "主治医生", "科室", "职称"};
        tableModel = new DefaultTableModel(columns, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        recordTable = new JTable(tableModel);
        add(new JScrollPane(recordTable), BorderLayout.CENTER);
        
        // 创建详情面板
        JPanel detailPanel = new JPanel(new BorderLayout());
        detailPanel.setBorder(BorderFactory.createTitledBorder("详细信息"));
        JTextArea diagnosisArea = new JTextArea(4, 40);
        JTextArea prescriptionArea = new JTextArea(4, 40);
        diagnosisArea.setEditable(false);
        prescriptionArea.setEditable(false);
        
        JPanel textPanel = new JPanel(new GridLayout(2, 1, 5, 5));
        textPanel.add(new JScrollPane(diagnosisArea));
        textPanel.add(new JScrollPane(prescriptionArea));
        detailPanel.add(textPanel, BorderLayout.CENTER);
        
        add(detailPanel, BorderLayout.SOUTH);
        
        // 添加表格选择事件
        recordTable.getSelectionModel().addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting() && recordTable.getSelectedRow() != -1) {
                int row = recordTable.getSelectedRow();
                String recordId = (String) tableModel.getValueAt(row, 0);
                loadRecordDetails(recordId, diagnosisArea, prescriptionArea);
            }
        });
        
        // 加载记录
        loadRecords();
    }
    
    private void loadRecords() {
        tableModel.setRowCount(0);
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = ConnectionUtils.getConn();
            String sql = "SELECT m.id, m.create_time, m.diagnosis, m.prescription, " +
                        "u.name as doctor_name, d.department, d.doctorRank " +
                        "FROM medical_record m " +
                        "JOIN doctor d ON m.doctor_id = d.id " +
                        "JOIN user u ON d.user_id = u.id " +
                        "WHERE m.patient_id = ? " +
                        "ORDER BY m.create_time DESC";
            
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, patientId);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                Object[] row = {
                    rs.getString("id"),
                    rs.getTimestamp("create_time"),
                    rs.getString("doctor_name"),
                    rs.getString("department"),
                    rs.getString("doctorRank")
                };
                tableModel.addRow(row);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "加载就医记录失败：" + ex.getMessage());
        } finally {
            ConnectionUtils.close(conn, pstmt, rs);
        }
    }
    
    private void loadRecordDetails(String recordId, JTextArea diagnosisArea, JTextArea prescriptionArea) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = ConnectionUtils.getConn();
            String sql = "SELECT diagnosis, prescription FROM medical_record WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, recordId);
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                diagnosisArea.setText("诊断结果：\n" + rs.getString("diagnosis"));
                prescriptionArea.setText("处方信息：\n" + rs.getString("prescription"));
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "加载详细信息失败：" + ex.getMessage());
        } finally {
            ConnectionUtils.close(conn, pstmt, rs);
        }
    }
} 